#include "management.h"

Management::Management()
{
    handlemanage=new handle_manage();
    handle_thread=new QThread();
    handlemanage->moveToThread(handle_thread);
    handle_thread->start();
}


Management::~Management()
{
    handlemanage->deleteLater();            //关闭handlemanage
    handle_thread->quit();
    handle_thread->wait();
    handle_thread->deleteLater();
    l_server->deleteLater();                //关闭localserver
    l_server_thread->quit();
    l_server_thread->wait();
    l_server_thread->deleteLater();
    Common::tcp_server_SocketMapLock.lockForWrite();  //关闭tcpsocket
    QMap  <tcp_socket*,M_Socket*>::const_iterator i;
    for (i = Common::tcp_server_SocketMap.constBegin(); i != Common::tcp_server_SocketMap.constEnd(); ++i) {    //对QMAP进行遍历
         delete i.value();
         i.key()->deleteLater();
    }
    Common::tcp_server_SocketMap.clear();
    Common::tcp_server_SocketMapLock.unlock();
    process.clear();
    mylog.clear();
}

void Management::localServer_init(QString listenName)
{
    qDebug()<<"本地Server模块初始化";
    this->localServer_name=listenName;
    l_server=new local_server(localServer_name);
    l_server->set_handle(handlemanage);
}

bool Management::localServer_start()
{
    if(l_server!=NULL)
    {
        qDebug()<<"开启本地Server模块";
        bool result=l_server->start_server();
        l_server_thread=new QThread();
        l_server->moveToThread(l_server_thread);
        l_server_thread->start();
        return result;
    }
    else
    {
        qDebug()<<"请初始化localserver!";
        return false;
    }
}

void Management::tcp_init(QString ip, int port)
{
    qDebug()<<"初始化Socket,连接IP地址为:"<<ip<<"端口为:"<<QString::number(port);
    tcp_socket *t_socket=new tcp_socket(ip,port);
    connect(t_socket,SIGNAL(send_ReplyData(QString,QString,QString)),handlemanage,SLOT(receive(QString,QString,QString)));
    connect(handlemanage,SIGNAL(t_SocketSendData(QString,QString,QString,tcp_socket*,emit_type,QString)),t_socket,SLOT(send_messagefromslot(QString,QString,QString,tcp_socket*,emit_type,QString)));
    M_Socket *m_socket=new M_Socket();
    m_socket->Stype=Tsocket;
    m_socket->t_socket=t_socket;
    Common::tcp_server_SocketMapLock.lockForWrite();
    Common::tcp_server_SocketMap[t_socket]=m_socket;
    Common::tcp_server_SocketMapLock.unlock();
}


void Management::Handle_Setcount(int threadcount)
{
    qDebug()<<"处理模块初始化,当前处理线程个数为:"<<QString::number(threadcount);
    this->handlemanage->setHandleCount(threadcount);
}

void Management::Handle_Start()
{
    qDebug()<<"开启处理模块";
    this->handlemanage->init();
}


bool Management::process_start()
{
    qDebug()<<"开启进程模块";
    process.data()->start_process();
    return true;
}

void Management::process_init()
{
    qDebug()<<"进程模块初始化";
    process=QSharedPointer<process_exe>(new process_exe());
    connect(l_server,SIGNAL(restart_localcaculate()),process.data(),SLOT(local_restart()));
}

void Management::Log_init(QString logname)
{
    qDebug()<<"日志模块初始化";
    mylog = QSharedPointer<Log>(new Log());
    mylog.data()->init(logname);
}

void Management::StartLog()
{
    qDebug()<<"开启日志模块";
    mylog.data()->StartLog();
}

void Management::Configur_init(QString Filename)
{
    qDebug()<<"配置模块初始化";
    mysetting = QSharedPointer<Setting>(new Setting());
    mysetting.data()->init(Filename);
}

void Management::StartConfigur()
{
    qDebug()<<"启动配置模块";
    mysetting.data()->Start();
}
